package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.Result;
import com.itheima.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/report")
@Slf4j
public class reportController {
    @Autowired
    private ReportService reportService;
    @GetMapping("/getMemberReport")
    public Result report(){
        Map<String, Object> map = reportService.getMemberReport();

        return new Result(map!=null?true:false,map!=null? MessageConstant.GET_MEMBER_NUMBER_REPORT_SUCCESS:MessageConstant.GET_MEMBER_NUMBER_REPORT_FAIL,map);
    }
    @GetMapping("/getSetmealReport")
    public Result getSetmealReport(){
        Map<String,Object> map = reportService.getSetmealReport();
        return new Result(map!=null?true:false,map!=null? MessageConstant.GET_SETMEAL_COUNT_REPORT_SUCCESS:MessageConstant.GET_SETMEAL_COUNT_REPORT_FAIL,map);

    }

    @GetMapping("/getBusinessReportData")
    public Result getBusinessReportData() throws Exception {
        Map<String,Object> map = reportService.getBusinessReportData();
        return new Result(map!=null?true:false,map!=null? MessageConstant.GET_BUSINESS_REPORT_SUCCESS:MessageConstant.GET_BUSINESS_REPORT_FAIL,map);

    }
    /**
     * 导出Excel
     * @return
     */
    @GetMapping("/exportBusinessReport")
    public Result exportBusinessReport(HttpServletRequest request, HttpServletResponse response){
        try {
            Map<String,Object> map = reportService.getBusinessReportData();
            String reportDate = (String) map.get("reportDate");
            Integer todayNewMember = (Integer) map.get("todayNewMember");
            Integer totalMember = (Integer) map.get("totalMember");
            Integer thisWeekNewMember = (Integer) map.get("thisWeekNewMember");
            Integer thisMonthNewMember = (Integer) map.get("thisMonthNewMember");
            Integer todayOrderNumber = (Integer) map.get("todayOrderNumber");
            Integer todayVisitsNumber = (Integer) map.get("todayVisitsNumber");
            Integer thisWeekOrderNumber = (Integer) map.get("thisWeekOrderNumber");
            Integer thisWeekVisitsNumber = (Integer) map.get("thisWeekVisitsNumber");
            Integer thisMonthOrderNumber = (Integer) map.get("thisMonthOrderNumber");
            Integer thisMonthVisitsNumber = (Integer) map.get("thisMonthVisitsNumber");
            List<Map> hotSetmeal = (List<Map>) map.get("hotSetmeal");

            //创建Excel文件
            String templateFilePath = "templates/report_template.xlsx";
            ClassPathResource resource = new ClassPathResource(templateFilePath);
            //读取文件内容
            InputStream inputStream = resource.getInputStream();
            XSSFWorkbook excel = new XSSFWorkbook(inputStream);
            XSSFSheet sheet = excel.getSheetAt(0);
            sheet.getRow(2).getCell(5).setCellValue(reportDate);
            sheet.getRow(4).getCell(5).setCellValue(todayNewMember);
            sheet.getRow(4).getCell(7).setCellValue(totalMember);
            sheet.getRow(5).getCell(5).setCellValue(thisWeekNewMember);
            sheet.getRow(5).getCell(7).setCellValue(thisMonthNewMember);
            sheet.getRow(7).getCell(5).setCellValue(todayOrderNumber);
            sheet.getRow(7).getCell(7).setCellValue(todayVisitsNumber);
            sheet.getRow(8).getCell(5).setCellValue(thisWeekOrderNumber);
            sheet.getRow(8).getCell(7).setCellValue(thisWeekVisitsNumber);
            sheet.getRow(9).getCell(5).setCellValue(thisMonthOrderNumber);
            sheet.getRow(9).getCell(7).setCellValue(thisMonthVisitsNumber);

            int row = 12;
            for (Map map1 : hotSetmeal) {
                String name = (String) map1.get("name");
                Long setmeal_count = (Long) map1.get("setmeal_count");
                BigDecimal proportion = (BigDecimal) map1.get("proportion");
                XSSFRow row1 = sheet.getRow(row++);
                row1.getCell(4).setCellValue(name);
                row1.getCell(5).setCellValue(setmeal_count);
                row1.getCell(6).setCellValue(proportion.doubleValue());
            }

            //输出流表格下载,
            OutputStream os = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition","attachment;filename=reportdata.xlsx");
            excel.write(os);
            os.flush();
            os.close();
            excel.close();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_BUSINESS_REPORT_FAIL);
        }
    }
}
